package com.cys.dm.rest.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cys.dm.dao.user.DmUserDao;
import com.cys.dm.entity.user.DmUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/user")
public class DmUserService {

    @Resource
    private DmUserDao dmUserDao;

    @RequestMapping(path = "/findByPhoneAndPassword")
    public DmUser findByPhoneAndPassword(@RequestParam("phone")String phone, @RequestParam("password")String password){
        QueryWrapper<DmUser> dmUserQueryWrapper = new QueryWrapper<>();
        dmUserQueryWrapper.eq("phone",phone);
        dmUserQueryWrapper.eq("password",password);
        return dmUserDao.selectOne(dmUserQueryWrapper);
    }

    @PostMapping(path = "/findByVerification")
    public DmUser findByVerification(@RequestParam("phone")String phone){
        QueryWrapper<DmUser> dmUserQueryWrapper = new QueryWrapper<>();
        dmUserQueryWrapper.eq("phone",phone);
        return dmUserDao.selectOne(dmUserQueryWrapper);
    };

    @PostMapping(path = "/finById")
    public DmUser findById(@RequestParam("userId")Long userId){
        QueryWrapper<DmUser> dmUserQueryWrapper = new QueryWrapper<>();
        dmUserQueryWrapper.eq("id",userId);
        return dmUserDao.selectOne(dmUserQueryWrapper);
    };

    /**
     * 根据用户Id修改用户信息
     * @param dmUser
     * @return
     */
    @PostMapping(path = "/updataUserById")
    public Integer updateUserById(@RequestBody DmUser dmUser){
        return dmUserDao.updateById(dmUser);
    }

    @PostMapping(path = "/saveUser")
    public Integer saveUser(@RequestBody DmUser dmUser){
        return dmUserDao.insert(dmUser);
    }

    @PostMapping(path = "/findByPhone")
    public DmUser findByPhone(@RequestParam("phone") String phone){
        QueryWrapper<DmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return dmUserDao.selectOne(queryWrapper);
    }

}
